/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.market.test.server.service.impl.UserServiceImpl
 * @Create on：2024/3/8
 * @Author：liuchang
 */
package com.market.test.server.service.impl;

import com.market.test.common.domain.User;
import com.market.test.server.mapper.UserMapper;
import com.market.test.server.service.UserService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>ClassName：UserServiceImpl</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/8</p >
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Map<String, User> queryUserByIdBatch(ArrayList<UserWrapBatchService.Request> userReqs) {
        //全部参数
        List<Integer> userId = userReqs.stream().map(UserWrapBatchService.Request::getUserId).collect(Collectors.toList());

        List<User> list=userMapper.selectAll(userId);

        //stream流分组 可以根据给定的分类函数对数据进行分组
        Map<Integer, List<User>> userGroup = list.stream().collect(Collectors.groupingBy(User::getUserId));

        HashMap<String, User> map = new HashMap<>();

        userReqs.forEach(val->{
            List<User> userList = userGroup.get(val.getUserId());

            if (!Collections.isEmpty(userList)){
                map.put(val.getRequestId(),userList.get(0));

            }else {
                map.put(val.getRequestId(),null);
            }

        });
        return map;
    }
}
